<?php
namespace app\manager\controller;

use app\common\lib\AdminController;
use app\manager\model\ManagerModel;
use app\manager\model\validate\ManagerValidate;

class ManagerController extends AdminController
{
    public function index()
    {
        if ($this->request->isAjax()) {
            return $this->getList();
        }
        return $this->fetch();
    }

    private function getList() {
        $limit = input('limit','0');
        $offset = input('offset','0');
        $sortName = input('sortName','id');
        $sortOrder = input('sortOrder','desc');
        $keyword = input('keywords','');

        $model = new ManagerModel();
        if ($keyword) {
            $count = $model->where('username', 'like', '%' . $keyword . '%')->count();
            $list = $model
                ->where('username', 'like', '%' . $keyword . '%')
                ->order([$sortName => $sortOrder])
                ->limit($offset . ',' . $limit)
                ->select();
        } else {
            $count = $model->field('id')->count(); //查询满足要求的总记录数
            $list = $model
                ->order(array($sortName=>$sortOrder))
                ->limit($offset.','.$limit)
                ->select();
        }

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = $_REQUEST;
            $data['created_time'] = date('Y-m-d H:i:s', time());
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $validateM = new ManagerValidate();
            if (!$validateM->scene('add')->check($data)) {
                return json(['code' => 0, 'msg' => $validateM->getError()]);
            }

            $model = new ManagerModel();
            $data['password'] =$model->password($data['password']);
            $res = $model->allowField(true)->save($data);
            if (!$res) {
                return json(['code' => 0, 'msg' => $model->getError()]);
            }
            return json(['code' => 1, 'msg' => 'success']);
        }

        $this->assign('status_list', ManagerModel::statusList());
        return $this->fetch();
    }


    public function update()
    {
        $data = $_REQUEST;
        $model = new ManagerModel();

        if (request()->isPost()) {
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $validateM = new ManagerValidate();
            if (!$validateM->scene('update')->check($data)) {
                return json(['code' => 0, 'msg' => $validateM->getError()]);
            }

            //$data['password'] =$model->password($data['password']);
            $res = $model->allowField(true)->save($data, ['id' => $data['id']]);
            if (!$res) {
                return json(['code' => 0, 'msg' => $model->getError()]);
            }
            return json(['code' => 1, 'msg' => 'success']);
        }

        $user = $model->where(['id' => $data['id']])->find()->getData();
        $this->assign('Rs', $user);
        $this->assign('status_list', ManagerModel::statusList());
        return $this->fetch();
    }

    public function lock() {
        $data = $_REQUEST;
        $model = new ManagerModel();
        $m = $model->where(['id' => $data['id']])->find()->getData();
        $status = $model::STATUS_OK;
        if ($m['status'] == $model::STATUS_OK) {
            $status = $model::STATUS_LOCK;
        }
        $model->where(['id' => $data['id']])->update(['status' => $status]);
        return json(['code' => 1, 'msg' => 'success']);
    }
}
